package com.klun.project.common.constans.utils;

import com.klun.project.common.constans.entity.ListNode;
import com.klun.project.common.constans.entity.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class ParseUtils {

	// [[0,0,0],[0,1,0],[0,0,0]];
	public static int[][] stringToArrayInt(String a) {
		String[] lists = a.split("],\\[");
		int m = lists.length;
		int n = lists[0].split(",").length;
		int[][] res = new int[lists.length][lists[0].split(",").length];
		for (int i = 0; i < m; i++) {
			String[] params = lists[i].split(",");
			for (int j = 0; j < n; j++) {
				String param = rmSymbol(params[j]);
				res[i][j] = Integer.valueOf(param);
			}
		}
		return res;
	}

	// [[2],[3,4],[6,5,7],[4,1,8,3]];
	public static List<List<Integer>> stringToIntLists(String a) {
		String[] lists = a.split("],\\[");
		List<List<Integer>> lls = new ArrayList<>();
		for (int i = 0; i < lists.length; i++) {
			String[] params = lists[i].split(",");
			ArrayList<Integer> ls = new ArrayList<Integer>();
			for (int j = 0; j < params.length; j++) {
				String param = rmSymbol(params[j]);
				ls.add(Integer.valueOf(param));
			}
			lls.add(ls);
		}
		return lls;
	}

	//root = [5,1,4,null,null,3,6]
	public static TreeNode bTreeAddString(String t) {
		String[] split = t.split(",");
		TreeNode tn = new TreeNode(Integer.parseInt(rmSymbol(split[0])));
		for (int i = 1; i < split.length; i++) {
			String s = rmSymbol(split[i]);
			if (s == "" || null == s || "null".equals(s)) {
				continue;
			}
			tn.add(Integer.parseInt(s));
		}
		return tn;
	}

	//root = [5,1,4,null,null,3,6]
	public static TreeNode ArrayToBTree(String t) {
		String[] split = rmSymbol(t).split(",");
		TreeNode node = new TreeNode();
		atbDfs(split, 0, node);
		return node;
	}

	//root = [5,1,4,3,6]
	public static ListNode ArrayToListNode(String t) {
		String[] split = rmSymbol(t).split(",");
		ListNode res = new ListNode(Integer.parseInt(split[0]));
		ListNode node = res;
		for (int i = 1; i < split.length; i++) {
			node.next = new ListNode(Integer.parseInt(split[i]));
			node = node.next;
		}
		return res;
	}

	public static void atbDfs(String[] t, int i, TreeNode node) {
		if (t[i].equals("null")) {
			return;
		}
		node.val = Integer.parseInt(t[i]);
		if (t.length > 2 * i + 1 && !t[2 * i + 1].equals("null")) {
			TreeNode left = new TreeNode();
			node.left = left;
			atbDfs(t, 2 * i + 1, left);
		}
		if (t.length > 2 * i + 2 && !t[2 * i + 2].equals("null")) {
			TreeNode right = new TreeNode();
			node.right = right;
			atbDfs(t, 2 * i + 2, right);
		}
	}

	// 莫里斯中序遍历
	public static void morrisTraveral(TreeNode root) {
		TreeNode temp = null;
		while (root != null) {
			if (root.left != null) {
				temp = root.left;
				while (temp.right != null && temp.right != root) {
					temp = temp.right;
				}
				if (temp.right == null) {
					temp.right = root;
					root = root.left;
				} else { // 到这个逻辑解除右侧指到到root的指针
					temp.right = null;
					root = root.right; // 回到下一个指针
				}
			} else {
				root = root.right;
			}
		}
	}


	public static String rmSymbol(String a) {
		StringBuilder sb = new StringBuilder();
		for (char s : a.toCharArray()) {
			if (s == '[' || s == ']' || s == '(' || s == ')' || s == '{' || s == '}') {
				continue;
			}
			sb.append(s);
		}
		return sb.toString();
	}

	public static void main(String[] args) {

	}

}
